package com.xy.xylive.common;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.symmetric.AES;

@Component
public class AESUtil {
    @Value("${crypto.encryption-aes-key}")
    private String aesKey;
    
    private AES aes;

    /**
     * 初始化aes工具
     */
    @PostConstruct
    public void init() {
        byte[] key = HexUtil.decodeHex(aesKey);
        aes = new AES("ECB", "PKCS7Padding", key);
    }

    /**
     * 加密
     * @param data
     * @return
     */
    public String encrypt(String data) {
        return aes.encryptBase64(data);
    }

    /**
     * 解密
     * @param data
     * @return
     */
    public String decrypt(String data) {
        String decrypt = aes.decryptStr(Base64.decode(data));
        return decrypt;
    }
}
